/*
 * =====================================================================================
 *
 *       Filename:  util.h
 *
 *    
 *
 *        Version:  1.0
 *        Created:  11/16/2011 04:09:59 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */
#ifndef _UTIL_T_
#define _UTIL_T_
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <map>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define SERVPORT 3333
#define MAXDATASIZE 1024*1024*1024
#define SIZE 1024
#define BACKLOG 10

using namespace std;

typedef struct _data_t 
{
    string data;
//    long pos;
//    long length;
    bool isFull;
}data_t;

typedef struct _ret_t_
{
	unsigned long long total;
	unsigned long long cortotal;
	unsigned long long errtotal;
	unsigned long long errConTotal;
}ret_t;

int read_data(int sock_fd,char *buffer, int buffer_len,string &errorInfo);
int write_data(int sock_fd,const char *buffer, int buffer_len,string &errorInfo);
void *stress(void* arg);
void showResult();
class Request
{
    public:
        int doPost(int sockfd,string page,string &data,string &host,int port);
        //int doPost(int sockfd,string page,const char * data,string &host,int port);
        int doGet(int sockfd,string page,string data,string host,int port);
        int getStatus(char *response);
        int work(unsigned int);
        string errorInfo;
        static void openFile();
        static vector<data_t> vecData;
        static string strFileName;
        static string strLogName;
        static string strIp;
        static string strPage;
        static int intPort;
        static std::ofstream stresslog;
        static bool stressIsRunning;
        static unsigned long ratio;
        static unsigned long stressCount;
        static unsigned long stressInterval;
        static unsigned long stressThreadnum;
        static FILE *fin;
        static pthread_mutex_t _intReponseTimeMtx;
        static std::map<int,int> _responseTimeMap;
        static unsigned int _intMaxResponseTime,_intMinResponseTime,_totalResponseTime;
        static ret_t ret;    
        static struct timeval start,end;
        static void addIntResponseTime(struct timeval &startTime,struct timeval &endtime);

    private:
        //FILE *fin;
        unsigned int parallel;
};

#endif
